package com.ugc.harmony.clientai.lang.request
/**
 * 文本生成参数数据类
 * 用于配置和控制文本生成模型的行为
 */
data class GenerateParams(
    // 控制输出长度的参数
    val maxLength: Int = 100,

    // 控制生成策略的参数
    val doSample: Boolean = true,
    val temperature: Double = 0.3,
    val topK: Int = 50,

    val numBeams: Int = 1,
    val penaltyAlpha: Double = 0.0,


    val topP: Double = 1.0,
    val typicalP: Double = 1.0,
    val diversityPenalty: Double = 0.0,
    val repetitionPenalty: Double = 1.0,
    val lengthPenalty: Double = 1.0,
    val randomSeed: Long = 0L,

    // 原始数据映射
    var dtMap: Map<String, String> = emptyMap()
) {
    companion object {
        /**
         * 从Map创建GenerateParams对象
         * @param map 包含生成参数的映射，键名与类属性对应
         * @return 解析后的GenerateParams实例，无法解析的值使用默认值
         */
        fun fromMap(map: Map<String, String>): GenerateParams {
            return GenerateParams(
                maxLength = map["max_length"]?.toIntOrNull() ?: 100,
                doSample = map["do_sample"]?.toBooleanStrictOrNull() ?: true,
                numBeams = map["num_beams"]?.toIntOrNull() ?: 1,
                penaltyAlpha = map["penalty_alpha"]?.toDoubleOrNull() ?: 0.0,
                temperature = map["temperature"]?.toDoubleOrNull() ?: 0.3,
                topK = map["top_k"]?.toIntOrNull() ?: 50,
                topP = map["top_p"]?.toDoubleOrNull() ?: 1.0,
                typicalP = map["typical_p"]?.toDoubleOrNull() ?: 1.0,
                diversityPenalty = map["diversity_penalty"]?.toDoubleOrNull() ?: 0.0,
                repetitionPenalty = map["repetition_penalty"]?.toDoubleOrNull() ?: 1.0,
                lengthPenalty = map["length_penalty"]?.toDoubleOrNull() ?: 1.0,
                randomSeed = map["random_seed"]?.toLongOrNull() ?: 0L
            ).apply { this.dtMap = map }
        }
    }

    /**
     * 将对象转换回Map形式
     * @return 包含所有生成参数的Map
     */
    fun toMap(): Map<String, String> {
        return mapOf(
            "max_length" to maxLength.toString(),
            "do_sample" to doSample.toString(),
            "num_beams" to numBeams.toString(),
            "penalty_alpha" to penaltyAlpha.toString(),
            "temperature" to temperature.toString(),
            "top_k" to topK.toString(),
            "top_p" to topP.toString(),
            "typical_p" to typicalP.toString(),
            "diversity_penalty" to diversityPenalty.toString(),
            "repetition_penalty" to repetitionPenalty.toString(),
            "length_penalty" to lengthPenalty.toString(),
            "random_seed" to randomSeed.toString()
        )
    }

    fun parseNativeKVSerializes(): String {
        val paramsToSerialize = listOf(
            "max_length" to maxLength,
            "do_sample" to if (doSample) 1 else 0, // 布尔值转换为 0 或 1[6](@ref)
            "num_beams" to numBeams,
            "penalty_alpha" to penaltyAlpha,
            "temperature" to temperature,
            "top_k" to topK,
            "top_p" to topP,
            "typical_p" to typicalP,
            "diversity_penalty" to diversityPenalty,
            "repetition_penalty" to repetitionPenalty,
            "length_penalty" to lengthPenalty,
            "random_seed" to randomSeed
        )

        return paramsToSerialize.joinToString(";") { (key, value) ->
            "$key:$value"
        }
    }
}